package com.briup.smartcity_group1.exception.handler;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.briup.smartcity_group1.exception.ServiceException;
import com.briup.smartcity_group1.utils.result.Result;
import com.briup.smartcity_group1.utils.result.ResultCode;
import com.briup.smartcity_group1.utils.result.ResultUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/** 全局异常处理器
 * @author congee(congee02 @ 163.com)
 * @date 1/3/2023 4:02 PM
 */
@RestControllerAdvice(basePackages = {"com.briup.smartcity_group1.web.controller"})
public class ExceptionGlobalHandler {

    @ExceptionHandler(Exception.class)
    public Result handle(Exception e) {
        e.printStackTrace();
        if (e instanceof ServiceException) {
            return ResultUtils.error((ServiceException) e);
        }
        if (e instanceof JWTDecodeException) {
            return ResultUtils.error(ResultCode.USER_INVALID_TOKEN);
        }
        return ResultUtils.error(ResultCode.INTERNAL_ERROR);
    }

}
